Skip to content

feat(view): add view metadata types and validation - #1069

Open
kamcheungting-db wants to merge 5 commits into
apache:mainfrom
kamcheungting-db:feat/view-metadata-types
Open

kamcheungting-db wants to merge 5 commits into
apache:mainfrom
kamcheungting-db:feat/view-metadata-types

Conversation

@kamcheungting-db

@kamcheungting-db kamcheungting-db commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Adds the Iceberg view format v1 metadata model — the first step of view support. It provides the types, validation, and lookup helpers for view definitions and their history. JSON serialization and catalog integration follow in later PRs.

The metadata model

ViewMetadata
|   view_uuid, location, format_version (must be 1), properties
|
|-- schemas:  [Schema by schema_id]
|
|-- versions: [ViewVersion]              current_version_id --> one of these
|     |   version_id, schema_id  -------------> must be a listed schema
|     |   timestamp, summary
|     |   default catalog + namespace     (namespace must be non-empty)
|     |   representations: [ViewRepresentation]
|     |         |
|     |         +-- SQL:     sql text + dialect   (non-empty, one per dialect)
|     |         +-- Unknown: type identifier       (non-empty)
|
+-- version_log: [timestamp + version_id] --> must be a listed version

All types compare by value.

Validation

ViewMetadata::Validate() returns the first violated invariant as ErrorKind::kInvalid and leaves the metadata unchanged:

  • Well-formed: format version 1; non-empty UUID and location; non-empty schemas and versions with unique, non-null IDs.
  • Referential: the current version is listed; every version references a listed schema; every version_log entry references a listed version.
  • Per version: non-empty default namespace; SQL representations have non-empty SQL and dialect, at most one per dialect (compared ASCII case-insensitively); unknown representations have a non-empty type identifier.

View schemas are validated independently of table-format schema restrictions, so projections keep the types and nullability of referenced columns.

CurrentVersion(), VersionById(), CurrentSchema(), and SchemaById() return the matching non-null entry, or ErrorKind::kNotFound when it is absent.

The change also adds StringUtils::EqualsIgnoreCaseAscii for dialect matching and registers the new sources, tests, and public headers with the build and the C++20 compatibility check.

Follow-ups

JSON serialization, preserving unknown representation payloads, metadata updates, and requirements.

Tests

view_metadata_test — 26 tests: valid metadata, multiple versions and dialects, unknown representation types, every validation failure with an asserted error kind (including version_log entries that reference unlisted versions), missing-entry lookups, equality, and representation helpers.

Validated locally: all 26 view tests pass with ASAN + UBSAN + leak detection, pre-commit is clean, and clang-tidy 22.1.8 (the version CI pins) reports no findings on changed lines.

Comment thread src/iceberg/view/view_metadata.cc Resolved
Deliver the view format v1 metadata model beside its table equivalent:

- ViewVersion carries version-id, schema-id, timestamp-ms, summary,
  representations, optional default catalog, and required default namespace.
- Representations are SQL (sql, dialect) or opaque unknown types preserved
  for round trips, modeled as a variant with GetSql and RepresentationType
  helpers.
- ViewMetadata carries view-uuid, format-version, location, schemas,
  current-version-id, versions, version-log, and properties, with
  CurrentVersion/VersionById/CurrentSchema/SchemaById accessors returning
  NotFound for missing entries.
- Validate() checks view-v1 invariants without applying table-format schema
  restrictions: supported format version, non-empty UUID/location, listed
  schemas/versions with unique IDs, current-version and schema references,
  non-empty default namespaces, non-empty SQL representations with at most
  one per dialect under ASCII case folding, and non-empty unknown
  representation types.
- Add StringUtils::EqualsIgnoreCaseAscii for the ASCII-only dialect folding;
  existing Unicode-aware EqualsIgnoreCase is unchanged.
- view_metadata_test validates synthetic metadata: every invariant violated
  one at a time with asserted error kinds, accepted shapes including unknown
  representations, accessor NotFound behavior, and equality.
@kamcheungting-db

Copy link
Copy Markdown
Contributor Author

@shangxinli @wgtmac

Could you help me review this first PR of view?

/// \return the SQL representation, or nullptr when the representation is not
/// a SQL representation. The pointer remains valid until the representation
/// is modified or destroyed.
ICEBERG_EXPORT const SqlViewRepresentation* GetSql(const ViewRepresentation& repr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a named SQL alternative implicitly creates a temporary variant. The pointer returned by GetSql dangles at the end of the statement. Please reject temporary variants in both borrowing helpers.

Comment thread src/iceberg/view_metadata.h Outdated
static constexpr int8_t kSupportedViewFormatVersion = 1;

/// An integer version number for the view format; must be 1.
int8_t format_version;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please default-initialize the scalar members. A default-constructed ViewMetadata reads an indeterminate format version on its first call to Validate().

Comment thread src/iceberg/view_metadata.cc Outdated
return Invalid("View version {} references unlisted schema ID {}",
version->version_id, version->schema_id);
}
if (version->default_namespace.levels.empty()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java can write an empty default namespace as []. Please allow that value and change the existing test to cover it.

if (schema == nullptr) {
return Invalid("View schemas must not be null");
}
if (!schema_ids.insert(schema->schema_id()).second) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A view schema with duplicate field IDs passes this check and then fails on field lookup. Please validate field ID uniqueness independently of table format-version restrictions.

dialects.push_back(sql->dialect);
} else {
const auto& unknown = std::get<UnknownViewRepresentation>(repr);
if (unknown.type.empty()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unknown representation tagged sql passes validation without a query or dialect. Please reserve the SQL discriminator for the SQL alternative, including the case variants recognized by Java.

Comment thread src/iceberg/view_metadata.cc Outdated
return Invalid("Unsupported view format version: {}",
static_cast<int>(format_version));
}
if (view_uuid.empty()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A non-empty string such as x passes as a view UUID. Please validate it with the existing UUID parser and return kInvalid for malformed values.

Comment thread src/iceberg/view_metadata.cc Outdated
version->version_id);
}
for (const auto& dialect : dialects) {
if (StringUtils::EqualsIgnoreCaseAscii(dialect, sql->dialect)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java treats Éngine and éngine as the same dialect. Please normalize each dialect before inserting it into a set and cover this pair in the duplicate-dialect test.

Comment thread src/iceberg/CMakeLists.txt Outdated
util/url_encoder.cc
util/uuid.cc)
util/uuid.cc
view_metadata.cc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding a view subdirectory.

Comment thread src/iceberg/type_fwd.h Outdated
class SnapshotSummaryBuilder;

/// \brief View metadata.
struct SqlViewRepresentation;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move them to view/type_fwd.h after the subdir is created.

…ation

- Move view metadata and version sources and headers into a view
  subdirectory with its own type_fwd.h.
- Reject temporary variants in GetSql and RepresentationType.
- Default-initialize scalar members of view metadata, versions, and
  history entries.
- Accept an empty default namespace, as Java writes it.
- Reject duplicate field IDs in view schemas.
- Reserve the SQL representation type for SQL representations,
  including Java's case-insensitive variants.
- Validate view UUIDs with the UUID parser.
- Normalize SQL dialects with Unicode case folding before checking
  for duplicates.

Co-authored-by: Isaac <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants